<template>
    <el-container>
      <!--头部-->
  <el-header><div><img class="img" src="../assets/60784f2cb09772dfe1b9147832d6a25.png" alt=""><span>电商后台管理系统</span></div>
    <el-button type="info" @click="logout">退出</el-button>
  </el-header>
<!--页面主体-->
  <el-container>
  <!--侧边栏-->
    <el-aside :width="iscollapse ? '64px' : '200px' ">
      <!--侧边栏-->
      <div class="toggle-button" @click="toggleCllapse()">|||</div>
    <el-menu unique-opened
    router
      background-color="#333744"
      text-color="#fff"
      active-text-color="blue" :collapse="iscollapse" :default-active="activePath">
      <!--一级菜单-->
      <el-submenu :index="item.id + ' ' " v-for="item in menulist" :key="item.id">
        <!--一级菜单模板-->
        <template slot="title">
          <!--图标-->
          <i class="iconsObj[item.id]"></i>
          <!--文本-->
      <span>{{ item.authName }}</span>
        </template>
        <!--二级菜单---->
          <el-menu-item @click="saveNavState('/'+subItem.path+'')" :index="'/'+subItem.path+''" v-for="subItem in item.children" :key="subItem.id"><i class="el-icon-menu"></i>{{ subItem.authName }}</el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <!--右侧主体-->
    <el-main>
      <!--路由占位符-->
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>

</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      iconsObj: {
        ' 125 ': 'iconfont icon-user',
        ' 103 ': 'iconfont icon-tijikongjian',
        ' 101 ': 'iconfont icon-shangpin',
        ' 102 ': 'iconfont icon-danju',
        ' 145 ': 'iconfont icon-baobiao'
      },
      iscollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('active')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(res)
    },
    //  点击按钮折叠
    toggleCllapse () {
      this.iscollapse = !this.iscollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-header{
  background-color: #333744;
  display:flex;
  justify-content:space-between;
  padding: 0;
  align-items: center;
  color: #eaedf1;
  font-size: 20px;
  >div{
    display: flex;
    align-items: center;
  }
}
.el-aside{
  background-color: #333744;
}
.el-main{
  background-color: #eaedf1;
}
.el-container{
  height:100%;
}
.img{
  border-radius: 50%;
  display: flex;
  align-items: center;
}
.el-menu{
  border-right:none
}
.toggle-button{
  background-color: #4a5064;
  font-size: 1px;
  color: #fff;
  text-align: center;
  letter-spacing: 1em;
  cursor: pointer;
}
</style>
